--UI工具

td.WIN_COLOR = cc.c4b(0, 0, 0, 200)             -- 弹窗的半透明背景
td.BTN_PRESSED_COLOR = cc.c3b(80, 80, 80)    -- 按钮按下的叠加颜色
td.GREEN = cc.c3b(160,254,49)
td.LIGHT_GREEN = cc.c3b(120,244,191)
td.BLUE = cc.c3b(7,179,236)
td.LIGHT_BLUE = cc.c3b(102, 238, 255)
td.DARK_BLUE = cc.c3b(7,168,197)                -- 用在大段文本的时候
td.RED = cc.c3b(254, 41, 29)                    -- 数量不够时的数字颜色
td.YELLOW = cc.c3b(255, 241, 83)
td.WHITE = cc.c3b(255, 255, 255)
td.BLACK = cc.c3b(0, 0, 0)
td.GRAY = cc.c3b(170, 170, 170)

td.PlayerColor = {
    td.BLUE,
    td.RED,
}

td.btnMap = {
    lanse_chang = {    enabled = "UI/button/lanse1_chang_button.png",    pressed = "UI/button/lanse2_chang_button.png",    disabled = "UI/button/huise_chang_button.png",},
    huangse_chang = {    enabled = "UI/button/huangse1_chang_button.png",    pressed = "UI/button/huangse2_chang_button.png",    disabled = "UI/button/huise_chang_button.png",},
    lanse_duan = {    enabled = "UI/button/lanse1_duan_button.png",    pressed = "UI/button/lanse2_duan_button.png",    disabled = "UI/button/huise_duan_button.png",},
    huangse_duan = {    enabled = "UI/button/huangse1_duan_button.png",    pressed = "UI/button/huangse2_duan_button.png",    disabled = "UI/button/huise_duan_button.png",},
    huangse_zheng = {    enabled = "UI/button/huangse1_zheng_button.png",    pressed = "UI/button/huangse2_zheng_button.png",    disabled = "UI/button/huise_zheng_button.png",},
}

function td.CreateBtn(data)
    data = data or { }
    if data.text == nil then 
        local text = data
        data = {text = text}
    end
    data.map = data.map or td.btnMap.lanse_chang
    local btn = ccui.Button:create(data.map.enabled, data.map.pressed, data.map.disabled)

    if data.text then
        td.BtnAddText(btn, data.text)
    end

    if data.cb then
        td.BtnAddTouch(btn, data.cb)
    end

    return btn
end

function td.BtnAddText(btn,data)    --接受传入data或者直接传入str
    local label = btn:getChildByName("text")  
    if data.str == nil then 
        local str = data
        data = {str = str}
    end

    if label then
        label:setString(data.str)
    else
        data.size = data.size or 24
        data.color = data.color or td.BLACK
	    data.pos = data.pos or cc.p(0.5,0.5)

        label = td.CreateLabel(data.str,data.color,data.size)

        label:setName("text")
        label:setAnchorPoint(0.5,0.5)
        td.AddRelaPos( btn , label , data.pos )
    end
end

function td.BtnAddTouch(btn, cb)
    btn:addTouchEventListener( function(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            cb(btn)
        end
    end )
end

function td.AddRelaPos(parent,child,pos,zorder)
    pos = pos or cc.p(0.5,0.5)
    zorder = zorder or 0
    local conSize = parent:getContentSize()
    child:setPosition(conSize.width*pos.x,conSize.height*pos.y)
    parent:addChild(child,zorder)
end

function td.CreateLabel(str,fColor,fSize,olColor,olSize,dimen)  --创建文本
	local text       = tostring(str)
    local font       = td.DEFAULT_FONT
    local size       = fSize or 24
    local color      = fColor or td.BLACK
    local textAlign  = cc.TEXT_ALIGNMENT_LEFT
    local textValign = cc.VERTICAL_TEXT_ALIGNMENT_TOP
    local dimensions = dimen or cc.size(0, 0)
	
	local label
    if cc.FileUtils:getInstance():isFileExist(font) then
        label = cc.Label:createWithTTF(text, font, size, dimensions, textAlign, textValign)
    else
        label = cc.Label:createWithSystemFont(text, font, size, dimensions, textAlign, textValign)
    end

    if label then
        label:setColor(color)
		if olColor then
			olSize = olSize or 1
			label:enableOutline(olColor,olSize)
		end
    end

    return label
end
--[[
    创建文本(参数列表版本)
    str,color,size,olColor,olSize,dimen,align,valign
]]
function td.CreateLabel2(data)
    data.color = data.color or cc.c3b(255,255,255)
    data.size = data.size or 20
	data.align = data.align or cc.TEXT_ALIGNMENT_LEFT
    data.valign = data.valign or cc.VERTICAL_TEXT_ALIGNMENT_TOP
	data.dimen = data.dimen or cc.size(0, 0)

    local textLabel
	if cc.FileUtils:getInstance():isFileExist(td.DEFAULT_FONT) then
		textLabel = cc.Label:createWithTTF(data.str, td.DEFAULT_FONT, data.size, data.dimen, data.align, data.valign)
	else
		textLabel = cc.Label:createWithSystemFont(data.str, td.DEFAULT_FONT, data.size, data.dimen, data.align, data.valign)
	end
    if textLabel then
        textLabel:setColor(data.color)
    end
    if data.olColor then
        data.olSize = data.olSize or 1
        textLabel:enableOutline(data.olColor,data.olSize)
    end

    return textLabel
end

function td.CreateEditBox(data) --输入框
    data.texture = data.texture or "UI/s9/9.png"
    data.size = data.size or cc.size(100, 50)
    data.textSize = data.textSize or 24
    data.textLength = data.textLength or 5
    data.placeHolder = data.placeHolder or "无说明"

    local EditBox = ccui.EditBox:create(data.size, data.texture)
    EditBox:setFontSize(data.textSize)
    EditBox:setMaxLength(data.textLength)
    EditBox:setPlaceHolder(data.placeHolder)

    if data.text then EditBox:setText(data.text) end

    return EditBox
end

function td.GetGridForPos(pos)    -- 根据当前坐标获取对应格点位置
    local width = td.Map.w
    local height = td.Map.h

    pos.gridX = math.ceil(pos.x / width)
    pos.gridY = math.ceil(pos.y / height)

    return pos
end

function td.GetPosForGrid(Grid)    -- 根据当前格点位置获取对应坐标
    Grid.gridX = Grid.gridX or Grid.x
    Grid.gridY = Grid.gridY or Grid.y

    local gridPos = td.GridPos[Grid.gridX][Grid.gridY] 
    Grid.x = gridPos.x_core
    Grid.y = gridPos.y_core

    return Grid
end

function td.newScale9Sprite(str,size,rect)
	local spr = display.newSprite(str,{scale9=true,capInsets=rect})
	spr:setContentSize(size)
	return spr
end